// compensated Lanczos2

#ifndef AXIS
    #define AXIS 0
#endif

#define PI acos(-1.)

sampler s0 : register(s0);
float2 dxdy : register(c0);

float4 main(float2 tex : TEXCOORD0) : COLOR
{
    float t = frac(tex[AXIS]);
#if (AXIS == 0)
    float2 pos = tex-float2(t, 0.);
#elif (AXIS == 1)
    float2 pos = tex-float2(0., t);
#else
    #error ERROR: incorrect AXIS.
#endif

    float4 Q1 = tex2D(s0, (pos+.5)*dxdy); // nearest original pixel to the left
    if(t) {
        // original pixels
#if (AXIS == 0)
        float4 Q0 = tex2D(s0, (pos+float2(-.5, .5))*dxdy);
        float4 Q2 = tex2D(s0, (pos+float2(1.5, .5))*dxdy);
        float4 Q3 = tex2D(s0, (pos+float2(2.5, .5))*dxdy);
#elif (AXIS == 1)
        float4 Q0 = tex2D(s0, (pos+float2(.5, -.5))*dxdy);
        float4 Q2 = tex2D(s0, (pos+float2(.5, 1.5))*dxdy);
        float4 Q3 = tex2D(s0, (pos+float2(.5, 2.5))*dxdy);
#endif
        float4 wset = float3(0., 1., 2.).yxyz+float2(t, -t).xxyy;
        float4 w = sin(wset*PI)*sin(wset*PI*.5)/(wset*wset*PI*PI*.5);

        float wc = 1.-dot(1., w); // compensate truncated window factor by bilinear factoring on the two nearest samples
        w.y += wc*(1.-t);
        w.z += wc*t;

        return w.x*Q0+w.y*Q1+w.z*Q2+w.w*Q3; // interpolation output
    }

    return Q1; // case t == 0. is required to return sample Q1, because of a possible division by 0.
}
